package org.zgy.stock.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClient;

import java.net.URI;

@Component
public class RestClientUtils {
    private static final Logger logger = LoggerFactory.getLogger(RestClientUtils.class);
    private final String baseUrl = "https://push2.eastmoney.com/";
    private final RestClient restClient;

    public RestClientUtils() {
        this.restClient = RestClient.builder()
                .baseUrl(baseUrl)
                .requestInterceptor((request, body, execution) -> {
                    request.getHeaders().add(HttpHeaders.HOST, "push2.eastmoney.com");
                    request.getHeaders().add(HttpHeaders.CONTENT_TYPE, "application/json");
                    request.getHeaders().add(HttpHeaders.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
                    request.getHeaders().add(HttpHeaders.CACHE_CONTROL, "max-age=0");
                    return execution.execute(request, body);
                })
                .build();
    }

    public <T> T get(String path, Class<T> responseType) {
        URI fullUri;
        try {
            fullUri = new URI(baseUrl).resolve(path);
        } catch (Exception e) {
            logger.error("Invalid URL for GET request: {}", path, e);
            throw new RuntimeException("Invalid URL", e);
        }
        logger.info("Sending GET request to: {}", fullUri.toString());

        try {
            T response = restClient.get()
                    .uri(path)
                    .retrieve()
                    .body(responseType);
            logger.info("Received response: {}", response);
            return response;
        } catch (Exception e) {
            logger.error("Error sending GET request to {}: {}", fullUri.toString(), e.getMessage(), e);
            throw e;
        }
    }

    public <T> ResponseEntity<T> post(String path, Object request, Class<T> responseType) {
        URI fullUri;
        try {
            fullUri = new URI(baseUrl).resolve(path);
        } catch (Exception e) {
            logger.error("Invalid URL for POST request: {}", path, e);
            throw new RuntimeException("Invalid URL", e);
        }
        logger.info("Sending POST request to: {} with body: {}", fullUri.toString(), request);
        try {
            ResponseEntity<T> response = restClient.post()
                    .uri(path)
                    .body(request)
                    .retrieve()
                    .toEntity(responseType);
            logger.info("Received response with status: {} and body: {}", response.getStatusCode(), response.getBody());
            return response;
        } catch (Exception e) {
            logger.error("Error sending POST request to {}: {}", fullUri.toString(), e.getMessage(), e);
            throw e;
        }
    }
}
